home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / SystemCode / CorePackages / comments.tcl < prev    next >
Encoding:
Text File  |  2000-11-26  |  19.8 KB  |  706 lines

  1. #  AlphaTcl - core Tcl engine
  2.  
  3. namespace eval comment {}
  4.  
  5. proc comment::Line {} {
  6.     global mode
  7.     if {![catch {comment::Characters Paragraph} chars]} {
  8.     set start [lindex $chars 0]
  9.     set end [lindex $chars 1]
  10.      if {[string trim $start] == [string trim $end]} {
  11.         insertPrefix
  12.     } else {
  13.         set ext  [file extension [win::CurrentTail]]
  14.         if {($mode == "C" || $mode == "C++") && $ext != ".h" && $ext != ".c"} {
  15.         insertPrefix
  16.         } else {
  17.                 if {[pos::compare [getPos] == [selEnd]]} {
  18.                     beginningOfLine
  19.                     insertText $start
  20.                     endOfLine
  21.                     insertText $end
  22.                     beginningOfLine
  23.                 } else {
  24.                     comment::Paragraph
  25.                 }
  26.         }
  27.     }
  28.     } else {
  29.     insertPrefix
  30.     }
  31. }
  32.  
  33. proc comment::undoLine {} {removePrefix}
  34.  
  35. proc comment::Box {} {
  36.  
  37.     # Preliminaries
  38.     if {[comment::GetRegion Box]} { return }
  39.     
  40.     set commentList [comment::Characters Box]
  41.     if {![llength $commentList]} { return }
  42.     
  43.     set begComment [lindex $commentList 0]
  44.     set begComLen [lindex $commentList 1]
  45.     set endComment [lindex $commentList 2]
  46.     set endComLen [lindex $commentList 3]
  47.     set fillChar [lindex $commentList 4]
  48.     set spaceOffset [lindex $commentList 5]
  49.  
  50.     set aSpace " "
  51.  
  52.     # First make sure we grab a full block of lines and adjust highlight
  53.  
  54.     set start [getPos]
  55.     set start [lineStart $start]
  56.     set end [selEnd]
  57.     set end [nextLineStart [pos::math $end - 1]]
  58.     select $start $end
  59.  
  60.     # Now get rid of any tabs
  61.     
  62.     if {[pos::compare $end < [maxPos]]} {
  63.     createTMark stopComment [pos::math $end + 1]
  64.     tabsToSpaces
  65.     gotoTMark stopComment
  66.     set end [pos::math [getPos] - 1]
  67.     removeTMark stopComment
  68.     } else {
  69.     tabsToSpaces
  70.     set end [maxPos]
  71.     }
  72.     select $start $end
  73.     set text [getText $start $end]
  74.     
  75. # Next turn it into a list of lines--possibly drop an empty 'last line'
  76.  
  77.     set lineList [split $text "\r\n"]
  78.     set numLines [llength $lineList]
  79.     if {[lindex $lineList end] == {} } {
  80.     set lineList [lrange $lineList 0 [expr {$numLines -2}]]
  81.     set numLines [llength $lineList]
  82.     }
  83.  
  84. # Find the longest line length and determine the new line length
  85.  
  86.     set maxLength 0
  87.     foreach thisLine $lineList {
  88.     set thisLength [string length $thisLine]
  89.     if { $thisLength > $maxLength } { 
  90.         set maxLength $thisLength 
  91.     }
  92.     }
  93.     set newLength [expr {$maxLength + 2 + 2*$spaceOffset}]
  94.     
  95.     # Now create the top & bottom bars and a blank line
  96.  
  97.     set topBar $begComment
  98.     for { set i 0 } { $i < [expr {$newLength - $begComLen}] } { incr i } {
  99.     append topBar $fillChar
  100.     }
  101.     set botBar ""
  102.     for { set i 0 } { $i < [expr {$newLength - $endComLen}] } { incr i } {
  103.     append botBar $fillChar
  104.     }
  105.     append botBar $endComment
  106.     set blankLine $fillChar
  107.     for { set i 0 } { $i < [expr {$newLength - 2}] } { incr i } {
  108.     append blankLine " "
  109.     }
  110.     append blankLine $fillChar
  111.     
  112.     # For each line add stuff on left and spaces and stuff on right for box sides
  113.     # and concatenate everything into 'text'.  Start with topBar; end with botBar
  114.  
  115.     set text $topBar\r$blankLine\r
  116.     
  117.     set frontStuff $fillChar
  118.     set backStuff $fillChar
  119.     for { set i 0 } { $i < $spaceOffset } { incr i } {
  120.     append frontStuff " "
  121.     set backStuff $aSpace$backStuff
  122.     }
  123.     set backStuffLen [string length $backStuff]
  124.     
  125.     foreach thisLine $lineList {
  126.     set thisLine $frontStuff$thisLine
  127.     set thisLength [string length $thisLine]
  128.     set howMuchPad [expr {$newLength - $thisLength - $backStuffLen}]
  129.     for { set j 0 } { $j < $howMuchPad } { incr j } {
  130.         append thisLine " "
  131.     }
  132.     append thisLine $backStuff
  133.     append text $thisLine \r
  134.     }
  135.     
  136.     append text $blankLine \r $botBar \r
  137.     
  138. # Now replace the old stuff, turn spaces to tabs, and highlight
  139.  
  140.     replaceText    $start $end $text
  141.     set    end [pos::math $start + [string length $text]]
  142.     frontSpacesToTabs $start $end
  143. }
  144.  
  145. proc comment::undoBox {} {
  146.  
  147. # Preliminaries
  148.     if {[comment::GetRegion Box 1]} { return }
  149.     
  150.     set commentList [comment::Characters Box]
  151.     if {![llength $commentList]} { return }
  152.     
  153.     set    begComment [lindex $commentList    0]
  154.     set    begComLen [lindex $commentList 1]
  155.     set    endComment [lindex $commentList    2]
  156.     set    endComLen [lindex $commentList 3]
  157.     set    fillChar [lindex $commentList 4]
  158.     set    spaceOffset [lindex $commentList 5]
  159.     
  160.     set aSpace " "
  161.     set aTab \t
  162.  
  163.     # First make sure we grab a full block of lines
  164.  
  165.     set start [getPos]
  166.     set start [lineStart $start]
  167.     set end [selEnd]
  168.     set end [nextLineStart [pos::math $end - 1]]
  169.     set text [getText $start $end]
  170.  
  171.     # Make sure we're at the start and end of the box
  172.  
  173.     set startOK [string first $begComment $text]
  174.     set endOK [string last $endComment $text]
  175.     set textLength [string length $text]
  176.     if { $startOK != 0 || ($endOK != [expr {$textLength-$endComLen-1}] || $endOK == -1) } {
  177.     alertnote "You must highlight the entire comment box, including the borders."
  178.     return
  179.     }
  180.     
  181.     # Now get rid of any tabs
  182.     
  183.     if {[pos::compare $end < [maxPos]] } {
  184.     createTMark stopComment [pos::math $end + 1]
  185.     tabsToSpaces
  186.     gotoTMark stopComment
  187.     set end [pos::math [getPos] - 1]
  188.     removeTMark stopComment
  189.     } else {
  190.     tabsToSpaces
  191.     set end [maxPos]
  192.     }
  193.     select $start $end
  194.     set text [getText $start $end]
  195.     
  196. # Next turn it into a list of lines--possibly drop an empty 'last line'
  197.  
  198. # VMD May'95: changed this code segment because it previously had problems
  199. # with empty lines in the middle of the text to be commented
  200.  
  201.     set lineList [split $text "\n\r"]
  202.     set ll [llength $lineList]
  203.     if { [lindex $lineList end] == {} } {
  204.     set lineList [lrange $lineList 0 [expr {$ll -2}] ]
  205.     }
  206.     set numLines [llength $lineList]
  207.  
  208. # end changes.
  209.     
  210. # Delete the first and last lines, recompute number of lines
  211.  
  212.     set lineList [lreplace $lineList [expr {$numLines-1}] [expr {$numLines-1}] ]
  213.     set lineList [lreplace $lineList 0 0 ]
  214.     set numLines [llength $lineList]
  215.     
  216.     # Eliminate 2nd and 2nd-to-last lines if they are empty
  217.  
  218.     set eliminate $fillChar$aSpace$aTab
  219.     set thisLine [lindex $lineList [expr {$numLines-1}]]
  220.     set thisLine [string trim $thisLine $eliminate]
  221.     if { [string length $thisLine] == 0 } {
  222.     set lineList [lreplace $lineList [expr {$numLines-1}] [expr {$numLines-1}] ]
  223.     }
  224.     set thisLine [lindex $lineList 0]
  225.     set thisLine [string trim $thisLine $eliminate]
  226.     if { [string length $thisLine] == 0 } {
  227.     set lineList [lreplace $lineList 0 0 ]
  228.     }
  229.     set numLines [llength $lineList]    
  230.     
  231. # For each line trim stuff on left and spaces and stuff on right and splice
  232.  
  233.     set dropFromLeft [expr {$spaceOffset+1}]
  234.     set text ""
  235.     foreach thisLine $lineList {
  236.     set thisLine [string trimright $thisLine $eliminate]
  237.     set thisLine [string range $thisLine $dropFromLeft end]
  238.     append text $thisLine \r
  239.     }
  240.         
  241.     # Now replace the old stuff, convert spaces back to tabs
  242.  
  243.     replaceText    $start $end $text
  244.     set end [pos::math $start + [string    length $text]]
  245.     frontSpacesToTabs $start $end
  246. }
  247.  
  248. ## 
  249.  # -------------------------------------------------------------------------
  250.  #     
  251.  # "comment::Characters" --
  252.  #    
  253.  #  Adds the 'general' purpose characters which are used to check if we're
  254.  #  in a comment block.  Also has a check for an array entry like this:
  255.  #    
  256.  #    set C++::commentCharacters(General) [list "*" "//"]
  257.  #    
  258.  #  If such an entry exists, it is returned.  This allows mode authors to
  259.  #  keep everything self-contained. 
  260.  # -------------------------------------------------------------------------
  261.  ##
  262. proc comment::Characters {purpose} {
  263.     global mode commentCharacters
  264.     global ${mode}::commentCharacters
  265.     # allows a mode to define these things itself.
  266.     if {[info exists ${mode}::commentCharacters(${purpose})]} {
  267.     return [set ${mode}::commentCharacters(${purpose})]
  268.     }    
  269.     if {[info exists commentCharacters(${mode}:${purpose})]} {
  270.     return $commentCharacters(${mode}:${purpose})
  271.     }    
  272.     switch -- $purpose {
  273.     "General" {
  274.         switch -- $mode {
  275.         "TeX" {return "%" }
  276.         "Text" {return "!" }
  277.         "Fort" {return "C" }
  278.         "Scil" {return "//" }
  279.         "Perl" -
  280.         "Tcl" {return "\#" }
  281.         "C" {return "*" }
  282.         "Java" -
  283.         "C++" {return [list "*" "//"] }
  284.         "HTML" {return "<!--"}
  285.         default {
  286.             return
  287.         }
  288.         }
  289.     }        
  290.     "Paragraph" {        
  291.         switch -- $mode {
  292.         "TeX" {return [list "%% " " %%" " % "] }
  293.         "Text" {return [list "!! " " !!" " ! "] }
  294.         "Fort" {return [list "CC " " CC" " C "] }
  295.         "Scil" {return [list "//" "//" "//"] }
  296.         "Perl" -
  297.         "Tcl" {return [list "## " " ##" " # "] }
  298.         "Java" -
  299.         "C" -
  300.         "C++" {return [list "/* " " */" " * "] }
  301.         "HTML" { return [list "<!--" "-->" "|" ] }
  302.         default {
  303.             error "I don't know what 'paragraph' comments should look like in this mode.  Sorry."
  304.         }
  305.         }
  306.     }
  307.     "Box" {
  308.         switch -- $mode {
  309.         "TeX" {return [list "%" 1 "%" 1 "%" 3] }
  310.         "Text" {return [list "!" 1 "!" 1 "!" 3] }
  311.         "Fort" {return [list "C" 1 "C" 1 "C" 3] }
  312.         "Scil" {return [list "//" 2 "//"  2 "//" 3] }
  313.         "Perl" -
  314.         "Tcl" {return [list "#" 1 "#" 1 "#" 3] }
  315.         "Java" -
  316.         "C" -
  317.         "C++" {return [list "/*" 2 "*/" 2 "*" 3] }
  318.         "HTML" { return [list "<!--" 4 "-->" 3 "|" 3] }
  319.         default {
  320.             error "I don't know what 'box' comments should look like in this mode.  Sorry."
  321.         }
  322.         }    
  323.     }
  324.     }    
  325.     
  326. }
  327.  
  328. ## 
  329.  # Default is to look for a paragraph to comment out.  If sent '1', then we
  330.  # look for a commented region to uncomment.
  331.  ##
  332. proc comment::GetRegion { purpose {uncomment 0 } } {
  333.     if {[pos::compare [getPos] != [selEnd]]} {
  334.     watchCursor
  335.     return 0
  336.     }
  337.     
  338.     # there's no selection, so we try and generate one
  339.     
  340.     set pos [getPos]
  341.     if {$uncomment} {
  342.     # uncommenting
  343.     set commentList [comment::Characters $purpose]
  344.     if { [llength $commentList] == 0 } { return 1}
  345.     switch -- $purpose {
  346.         "Box" {
  347.         set begComment [lindex $commentList 0]
  348.         set begComLen [lindex $commentList 1]
  349.         set endComment [lindex $commentList 2]
  350.         set endComLen [lindex $commentList 3]
  351.         set fillChar [lindex $commentList 4]
  352.         set spaceOffset [lindex $commentList 5]
  353.         
  354.         # get length of current line
  355.         set line [getText [lineStart $pos] [nextLineStart $pos] ]
  356.         set c [string trimleft $line]
  357.         set slen [expr {[string length $line] - [string length $c]}]
  358.         set start [string range $line 0 [expr {$slen -1 }] ]
  359.                 
  360.         set pos [getPos]
  361.                 
  362.         if { $start == "" } {
  363.             set p $pos
  364.             while { [string first $fillChar $line] == 0 && \
  365.               [expr {[string last $fillChar $line] + [string length $fillChar]}] \
  366.               >= [string length [string trimright $line]] } {
  367.             set p [nextLineStart $p]
  368.             set line [getText [lineStart $p] [nextLineStart $p]]
  369.             }
  370.             set end [lineStart $p]
  371.             
  372.             set p $pos
  373.             set line "${fillChar}"
  374.             while { [string first $fillChar $line] == 0 && \
  375.               [expr {[string last $fillChar $line] + [string length $fillChar]}] \
  376.               >= [string length [string trimright $line]] } {
  377.             set p [prevLineStart $p]
  378.             set line [getText [prevLineStart $p] [lineStart $p] ]
  379.             }
  380.             set begin [prevLineStart $p]
  381.             
  382.         } else {
  383.             set line "$start"
  384.             set p $pos
  385.             while { [string range $line 0 [expr {$slen -1}] ] == "$start" } {
  386.             set p [nextLineStart $p]
  387.             set line [getText [lineStart $p] [nextLineStart $p]]
  388.             }
  389.             set end [prevLineStart $p]
  390.             
  391.             set p $pos
  392.             set line "$start"
  393.             while { [string range $line 0 [expr {$slen -1}] ] == "$start" } {
  394.             set p [prevLineStart $p]
  395.             set line [getText [prevLineStart $p] [lineStart $p] ]
  396.             }
  397.             set begin [lineStart $p]
  398.         }
  399.         
  400.         set beginline [getText $begin [nextLineStart  $begin]]
  401.         if { [string first "$begComment" "$beginline" ] != $slen } {
  402.             message "First line failed"
  403.             return 1
  404.         }
  405.         
  406.         set endline [getText $end [nextLineStart $end]]
  407.         set epos [string last "$endComment" "$endline"]
  408.         incr epos [string length $endComment]
  409.         set s [string range $endline $epos end ]
  410.         set s [string trimright $s]
  411.         
  412.         if { $s != "" } {
  413.             message "Last line failed"
  414.             return 1
  415.         }
  416.         
  417.         set end [nextLineStart $end]
  418.         select $begin $end
  419.         #alertnote "Sorry auto-box selection not yet implemented"
  420.         }
  421.         "Paragraph" {
  422.         set begComment [lindex $commentList 0]
  423.         set endComment [lindex $commentList 1]
  424.         set fillChar [lindex $commentList 2]
  425.                 
  426.         ## 
  427.          # basic idea is search back and forwards for lines that
  428.          # don't begin the same way and then see if they match the
  429.          # idea of the beginning and end of a block
  430.          ##
  431.         
  432.         set line [getText [lineStart $pos] [nextLineStart $pos] ]
  433.         set chk [string range $line 0 [string first $fillChar $line]]
  434.         if { [string trimleft $chk] != "" } {
  435.             message "Not in a comment block"
  436.             return 1
  437.         }
  438.         regsub -all {    } $line " " line
  439.         set p [string first "$fillChar" "$line"]
  440.         set start [string range "$line" 0 [expr {$p + [string length $fillChar] -1}]]
  441.         set ll [comment::GetFillLines $start]
  442.         set begin [lindex $ll 0]
  443.         set end [lindex $ll 1]
  444.         
  445.         set beginline [getText $begin [nextLineStart  $begin]]
  446.         if {[string first "$begComment" "$beginline" ] != $p } {
  447.             message "First line failed"
  448.             return 1
  449.         }
  450.                 
  451.         set endline [getText $end [nextLineStart $end]]
  452.         set epos [string last "$endComment" "$endline"]
  453.         incr epos [string length $endComment]
  454.         set s [string range $endline $epos end ]
  455.         set s [string trimright $s]
  456.         
  457.         if { $s != "" } {
  458.             message "Last line failed"
  459.             return 1
  460.         }
  461.         #goto $end
  462.         set end [nextLineStart $end]
  463.         select $begin $end
  464.         }
  465.     }
  466.     } else {
  467.     # commenting out
  468.     set searchString "^\[ \t\]*\$"
  469.     set searchResult1 [search -s -f 0 -r 1 -n $searchString $pos]
  470.     set searchResult2 [search -s -f 1 -r 1 -n $searchString $pos]
  471.     if {[llength $searchResult1]} {
  472.         set posStart [pos::math [lindex $searchResult1 1] + 1]
  473.     } else {
  474.         set posStart [minPos]
  475.     }
  476.     if {[llength $searchResult2]} {
  477.         set posEnd [lindex $searchResult2 0]
  478.     } else {
  479.         set posEnd [pos::math [maxPos] + 1]
  480.         goto [maxPos]
  481.         insertText "\n"
  482.     }
  483.     select $posStart $posEnd
  484.     }
  485.     
  486.     set str "Do you wish to "
  487.     if {$uncomment} { append str "uncomment" } else { append str "comment out" }
  488.     append str " this region?"
  489.     return [expr {![dialog::yesno $str]}]
  490. }
  491.  
  492.  
  493.  
  494. proc comment::SameStart { line start } {
  495.     regsub -all "\t" $line " " line
  496.     if {[string first "$start" "$line"] == 0 } {
  497.     return 1
  498.     } else {
  499.     return 0
  500.     }
  501. }
  502.  
  503. proc comment::TextBlock {text} {
  504.     set cc [comment::Characters "Paragraph"]
  505.     set c [lindex $cc 2]
  506.     regsub -all "\[\r\n\]" $text "\r${c}" text
  507.     return "[lindex $cc 0]\r[lindex $cc 2]${text}\r[lindex $cc 1]\r"
  508. }
  509.  
  510. proc comment::GetFillLines { start } {
  511.     set pos [getPos]
  512.     regsub -all "\t" $start " " start
  513.     set line "$start"
  514.     
  515.     set p $pos
  516.     while { [comment::SameStart "$line" "$start"] } {
  517.     set p [nextLineStart $p]
  518.     set line [getText [lineStart $p] [nextLineStart $p]]
  519.     }
  520.     set end [lineStart $p]
  521.     
  522.     set p $pos
  523.     set line "$start"
  524.     while { [comment::SameStart "$line" "$start"] } {
  525.     set p [prevLineStart $p]
  526.     set line [getText [prevLineStart $p] [lineStart $p] ]
  527.     }
  528.     set begin [prevLineStart $p]
  529.     return [list $begin $end]
  530. }
  531.  
  532. ## 
  533.  # Author: Vince Darley    <mailto:vince@santafe.edu> 
  534.  ##
  535.  
  536. proc comment::Paragraph {} {
  537.  
  538. # Preliminaries
  539.     if {[comment::GetRegion Paragraph]} { return }
  540.     
  541.     set commentList [comment::Characters Paragraph]
  542.     if { [llength $commentList] == 0 } { return }
  543.  
  544.     set begComment [lindex $commentList 0]
  545.     set endComment [lindex $commentList 1]
  546.     set fillChar [lindex $commentList 2]
  547.     
  548.     
  549.     # First make sure we grab a full block of lines and adjust highlight
  550.     
  551.     set start [getPos]
  552.     set start [lineStart $start]
  553.     set end [selEnd]
  554.     set end [nextLineStart [pos::math $end - 1]]
  555.     select $start $end
  556.     
  557.     # Now get rid of any tabs
  558.     
  559.     if {[pos::compare $end < [maxPos]] } {
  560.         createTMark stopComment [pos::math $end + 1]
  561.         tabsToSpaces
  562.         gotoTMark stopComment
  563.         set end [pos::math [getPos] - 1]
  564.         removeTMark stopComment
  565.     } else {
  566.         tabsToSpaces
  567.         set end [maxPos]
  568.     }
  569.     select $start $end
  570.     set text [getText $start $end]
  571.     
  572.     # Next turn it into a list of lines--possibly drop an empty 'last line'
  573.  
  574.     set lineList [split $text "\r\n"]
  575.     set ll [llength $lineList]
  576.     if { [lindex $lineList end] == {} } {
  577.         set lineList [lrange $lineList 0 [expr {$ll -2}] ]
  578.     }
  579.     set numLines [llength $lineList]
  580.     
  581.     # Find left margin for these lines
  582.     set lmargin 100
  583.     foreach l $lineList {
  584.         set lm [expr {[string length $l] - [string length [string trimleft $l]]}]
  585.         if { $lm < $lmargin } { set lmargin $lm }
  586.     }
  587.     set ltext ""
  588.     for { set i 0 } { $i < $lmargin } { incr i } {
  589.         append ltext " "
  590.     }
  591.     
  592.     # For each line add stuff on left and concatenate everything into 'text'. 
  593.     
  594.     set text ${ltext}${begComment}\r
  595.     
  596.     foreach l $lineList {
  597.         append text ${ltext} ${fillChar} [string range $l $lmargin end] \r
  598.     }
  599.     append text ${ltext} ${endComment} \r
  600.     
  601.     # Now replace the old stuff, turn spaces to tabs, and highlight
  602.     
  603.     replaceText $start $end $text
  604.     set end [pos::math $start + [string length $text]]
  605.     frontSpacesToTabs $start $end
  606. }
  607.  
  608. ## 
  609.  # Author: Vince Darley    <vince@santafe.edu>
  610.  ##
  611.  
  612. proc comment::undoParagraph {} {
  613.  
  614.     # Preliminaries
  615.     if {[comment::GetRegion Paragraph 1]} { return }
  616.     
  617.     set commentList [comment::Characters Paragraph]
  618.     if { [llength $commentList] == 0 } { return }
  619.     
  620.     set begComment [lindex $commentList 0]
  621.     set endComment [lindex $commentList 1]
  622.     set fillChar [lindex $commentList 2]
  623.     
  624.     set aSpace " "
  625.     set aTab \t
  626.     
  627.     # First make sure we grab a full block of lines and adjust highlight
  628.     
  629.     set start [getPos]
  630.     set start [lineStart $start]
  631.     set end [selEnd]
  632.     set end [nextLineStart [pos::math $end - 1]]
  633.     select $start $end
  634.     set text [getText $start $end]
  635.     
  636.     # Find left margin for these lines
  637.     set l [string range $text 0 [string first "\r" $text] ]
  638.     set lmargin [expr {[string length $l] - [string length [string trimleft $l]]}]
  639.     
  640.     # Make sure we're at the start and end of the paragraph
  641.  
  642.     set startOK [string first $begComment $text]
  643.     set endOK [string last $endComment $text]
  644.     set textLength [string length $text]
  645.     if { $startOK != $lmargin || ($endOK != [expr {$textLength-[string length $endComment]-1}] || $endOK == -1) } {
  646.         alertnote "You must highlight the entire comment paragraph, including the tail ends."
  647.         return
  648.     }
  649.     
  650.     # Now get rid of any tabs
  651.     
  652.     if {[pos::compare $end < [maxPos]]} {
  653.         createTMark stopComment [pos::math $end + 1]
  654.         tabsToSpaces
  655.         gotoTMark stopComment
  656.         set end [pos::math [getPos] - 1]
  657.         removeTMark stopComment
  658.     } else {
  659.         tabsToSpaces
  660.         set end [maxPos]
  661.     }
  662.     select $start $end
  663.     set text [getText $start $end]
  664.     
  665.     # Next turn it into a list of lines--possibly drop an empty 'last line'
  666.     
  667.     set lineList [split $text "\r\n"]
  668.     set ll [llength $lineList]
  669.     if { [lindex $lineList end] == {} } {
  670.         set lineList [lrange $lineList 0 [expr {$ll -2}] ]
  671.     }
  672.     set numLines [llength $lineList]
  673.     
  674.     # Delete the first and last lines, recompute number of lines
  675.     
  676.     set lineList [lreplace $lineList [expr {$numLines-1}] [expr {$numLines-1}] ]
  677.     set lineList [lreplace $lineList 0 0 ]
  678.     set numLines [llength $lineList]
  679.     
  680.     # get the left margin
  681.     set lmargin [string first $fillChar [lindex $lineList 0]]
  682.     set ltext ""
  683.     for { set i 0 } { $i < $lmargin } { incr i } {
  684.         append ltext " "
  685.     }
  686.     
  687.     # For each line trim stuff on left and spaces and stuff on right and splice
  688.     set eliminate $fillChar$aSpace$aTab
  689.     set dropFromLeft [expr {[string length $fillChar] + $lmargin}]
  690.     set text ""
  691.     foreach thisLine $lineList {
  692.         set thisLine [string trimright $thisLine $eliminate]
  693.         set thisLine ${ltext}[string range $thisLine $dropFromLeft end]
  694.         append text $thisLine \r
  695.     }
  696.     
  697.     # Now replace the old stuff, turn spaces to tabs, and highlight
  698.     
  699.     
  700.     replaceText    $start $end $text
  701.     set    end [pos::math $start + [string length $text]]
  702.     frontSpacesToTabs $start $end
  703. }
  704.  
  705.  
  706.